
import {LoginApp,isQianfan,isWeiXin,navigateTo} from '@/until/QFH5.js'
import {getUrlParams} from '@/until/tool.js'
import qs from "qs"
import store from 'store'

let baseUrl  = process.env.NODE_ENV === 'development' ? '/development' : process.env.VUE_APP_BASE_API




const request = (options={})=>{
	// #ifdef MP-WEIXIN
	baseUrl = global.baseURI
	// #endif
	return new Promise((reslove,reject)=>{
		let actid = uni.getStorageSync('actid') || null
		if(actid==null) actid = 1
		let token = uni.getStorageSync(actid+'bearerToken') || null
		// #ifdef APP-PLUS || H5
		let bearerToken_expire = uni.getStorageSync(actid+'bearerToken_expire') || null
		
		if(bearerToken_expire==null || bearerToken_expire < Math.floor(new Date()/1000)){
			uni.setStorageSync(actid+'bearerToken','')
		}
		// #endif
		

		let plat = null
		let deviceid = uni.getStorageSync('dec') || ""
		if(isWeiXin()){plat = 'wx'}
		if(isQianfan()){
			plat= 'app'
			QFH5.getDeviceId(function(state,data){
			  if(state==1){
			    //获取设备号成功
			    deviceid = data.deviceid;//用户设备唯一ID并MD5加密 string
				uni.setStorageSync('dec',data.deviceid)
			  }
			})
		}
		// #ifdef MP-WEIXIN
			plat='mp'
		// #endif
		// console.log(options.url)
		// console.log('请求',actid,token)
		// uni.showLoading({
		// 	title:''
		// })
		uni.request({
			url:baseUrl+options.url,
			data:options.data,
			method:options.method,
			timeout:10000,
			header:{
				'content-type': 'application/x-www-form-urlencoded',
				'Authorization':'Bearer '+token,
				'Client-Platform':plat,
				'Qianfan-NewH5-ActID':actid,
				'deviceid':deviceid,
				'longPos':store.state.long,
				'latPos':store.state.lat,
			}
		}).then(data=>{
			// console.log(data)
			// uni.hideLoading()
			let [err,res] = data
			if(res&&res.data){
				let data = res.data
				let answerData = null
				switch(data.ret || data.code){
					case 0: reslove(res.data);break;
					case 1:
						 
						reslove(res.data)
						break;
					case 10010:
						//用户黑名单
						uni.showToast({title:'您不是受邀用户',icon:'none'});
						answerData = uni.getStorageSync('answerData') || null
						if(answerData!=null && answerData.plugin=='answer')
							reslove(res.data)
						break;
						
							// navigateTo({url:'/plugin/pages/404'})
					case 10011:
					//区域白名单
						uni.showToast({title:'您所在地区不能参与该活动',icon:'none'});
						answerData = uni.getStorageSync('answerData') || null
						if(answerData!=null && answerData.plugin=='answer')
							reslove(res.data)
						break;
						// reslove(res.data)
						// navigateTo({url:'/plugin/pages/404'})
					case 401:
						// uni.showToast({title:res.data.text,icon:'none'});
						break;
					case 404:
						let op = getUrlParams()
						if(!op.hasOwnProperty('web')){
							uni.showToast({title:res.data.text,icon:'none'});
						}
						reslove(res.data);
						break;
					case 500:
						uni.showToast({title:res.data.text,icon:'none'});
						reslove(res.data);
						break;
					default:
						reslove(res.data);
						break;
				}
				uni.hideLoading()
			}else if(res!=undefined){
				uni.showToast({
					title:'异常：请检查网络状态！',
					icon: 'none',
				})
			}else{
				console.log('請求出現問題')
			}

		}).catch(error=>{
			uni.showToast({
				title:error,
				icon: 'none',
			})
			reject(error)
		})
	})
}

export const post = (url,data)=>{
	let ops = {
		url:url,
		data:data || {},
		method:'post'
	}
	return request(ops)
}
//qs序列化 兼容老版本
export const postQS = (url,data)=>{
	let ops = {
		url:url,
		data:data || {},
		method:'post'
	}
	return request(ops)
}
export const get = (url,data)=>{
	let ops = {
		url:url,
		data:data,
		method:'get'
	}
	return request(ops)
}
//
// //序列化  超老版本
// export function getJson(data) {
// 	let obj = {}
// 	obj.params = data
// 	let postData = {}
// 	postData.data = JSON.stringify(obj)
// 	if(process.env.NODE_ENV === 'development'){
// 		//4186 主体：驴宝宝2015  3887主体：大番薯
// 	    //data.uid = 4186
// 		postData.uid = 4186
// 	}
// 	postData.product_version = '331' // 低版本的客户端做了兼容处理  330以下的版本 都放在desc_tag里面的 高版本的才放到了title_tag里面
//     return qs.stringify(postData)
// }
//
//客户端接口传参封装
export function getJson2(data) {
	let obj = {}
	obj.params = data
	let post_data = {}
	post_data.data = JSON.stringify(obj)
	return qs.stringify(post_data)
}

